﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class Recovery : System.Web.UI.Page
{
    PIMService pimService = new PIMService();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        try
        {
            PIM.API.Users user = pimService.CheckEmail(txtEmailAddress.Value);
            if (user != null)
            {

                //string userEncrypt = Crypto.Encrypt(user.UserID);
                //string passEncrypt = Crypto.Encrypt(user.Password);
                string userEncrypt = HttpUtility.UrlEncode(EncryptDecryptQueryString.Encrypt(user.UserID));
                string passEncrypt = HttpUtility.UrlEncode(EncryptDecryptQueryString.Encrypt(user.Password));
                string url = Common1.getHost() + "ChangePassWord.aspx?u=" + userEncrypt + "&p=" + passEncrypt;

                string _mailServer = "smtp.gmail.com";
                int _mailPort = 587;
                string _user = "planitmeal2013@gmail.com";
                string _pass = "11911431121176";
                string _mailFrom = "planitmeal2013@gmail.com";
                string body = "";
                body += "<p>Dear Member of PlanItMeal,</p>";
                body += "<p>Please run link to change password: " + url + "</p>";
                body += "<p>Please login again, if there is any issue please email to admin@planitmeal.com to support.</p>";
                body += "<p>Best Regards,</p>";
                body += "<p>Admin of PlanItMeal</p>";

                MailMessage mailMessage = new MailMessage();
                SmtpClient mailClient = new SmtpClient(_mailServer, _mailPort);
                mailClient.Timeout = 15000;
                mailClient.Credentials = new NetworkCredential(_user, _pass);
                mailClient.EnableSsl = true;

                mailMessage.IsBodyHtml = true;
                mailMessage.From = new MailAddress(_mailFrom);
                mailMessage.Subject = "ResetPassword";
                mailMessage.Body = body;

                mailMessage.To.Add(txtEmailAddress.Value);
                mailClient.Send(mailMessage);
                //att.Dispose();
                mailMessage.Dispose();

                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Email is sent, please check your email to view new password!')", true);
            }
            else
            {
                errorMessage.Text = "Email is not valid";
            }
        }
        catch { ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('The send email is fail!')", true); }
    }

}